package cn.itcast.item.controller;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.mqconstans.MqConstants;
import cn.itcast.item.service.ItemService;
import org.apache.ibatis.annotations.Update;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.InvalidParameterException;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;
    @GetMapping("/{id}")
    public Item findById1(@PathVariable Long id){
        Item byId = itemService.findById(id);
        return byId;
    }
    @PutMapping("stock/{itemId}/{num}")
    public void stock(@PathVariable Long itemId,@PathVariable Integer num){
    itemService.stock(itemId,num);
    }
    @PostMapping("list")
    public PageDTO<Item> find(@RequestBody SearchItemDTO searchItemDTO){
        return itemService.find(searchItemDTO);
    }
    @PostMapping("")
    public ResultDTO add(@RequestBody Item item){
        return itemService.add(item);
    }
    @PutMapping("status/{id}/{status}")
    public ResultDTO updateStatus(@PathVariable String id,@PathVariable String status){
        return itemService.updateStatus(id,status);
    }
    @PutMapping("")
    public ResultDTO update(@RequestBody Item item){
        return itemService.update(item);

    }
    @DeleteMapping("/{id}")
    public ResultDTO delete(@PathVariable Long id){
        return itemService.delete(id);
    }
    @PostMapping("/{id}")
    public Item findById(@PathVariable Long id){
        return itemService.findById(id);
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
//    @PostMapping()
//    public void saveHotel(@RequestBody Item item){
//        itemService.save(item);
//        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_UP_KEY,item.getId());
//        System.out.println("成功"+item.getId());
//    }
//    @PutMapping("/{id}")
//    public void updateById(@PathVariable Long id){
//        Item item = new Item();
//        item.setId(id);
//        itemService.updateById(item);
//        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DOWN_KEY,id);
//        System.out.println("成功"+id);
//    }

}
